﻿#Region "Copyright"
'————————————————————————————————————————
'	创建人员(Creator)    ：Huangyd
'	创建日期(Date)       ：2012-07-14
'   联系方式(Email)      ：aj-ayumi@163.com; gajayumi@gmail.com; ajayumi@me.com
'   描    述(Description)：
'
'	Copyright (C) 2012 ajayumi 版权所有
'————————————————————————————————————————
#End Region
Imports System.Runtime.InteropServices
Imports System.Windows.Forms

Namespace WinAPI

    Public Class User32Dll
        ''' <summary>
        ''' 该函数将指定的消息发送到一个或多个窗口。此函数为指定的窗口调用窗口程序，直到窗口程序处理完消息再返回。
        ''' 而函数PostMessage不同，将一个消息寄送到一个线程的消息队列后立即返回。
        ''' </summary>
        ''' <param name="hWnd">其窗口程序将接收消息的窗口的句柄。如果此参数为HWND_BROADCAST，
        ''' 则消息将被发送到系统中所有顶层窗口，包括无效或不可见的非自身拥有的窗口、被覆盖的窗口和弹出式窗口，
        ''' 但消息不被发送到子窗口。</param>
        ''' <param name="nMsg">指定被发送的消息。</param>
        ''' <param name="wParam">指定附加的消息指定信息。</param>
        ''' <param name="lParam">指定附加的消息指定信息。</param>
        ''' <returns>返回值指定消息处理的结果，依赖于所发送的消息。</returns>
        ''' <remarks>需要用HWND_BROADCAST通信的应用程序应当使用函数RegisterWindowMessage来为应用程序间的通信取得一个唯一的消息。</remarks>

        <DllImport("user32.dll", CharSet:=CharSet.Unicode, setlasterror:=False)> _
        Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal nMsg As Int32, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function

        <DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
        Public Shared Function CreateWindowEx(ByVal dwExStyle As UInteger, ByVal lpClassName As String, ByVal lpWindowName As String, ByVal dwStyle As UInteger, ByVal x As Integer, ByVal y As Integer, _
    ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal hWndParent As IntPtr, ByVal hMenu As IntPtr, ByVal hInstance As IntPtr, ByVal lpParam As IntPtr) As IntPtr
        End Function


        <DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
        Public Shared Function RegisterClassEx(<[In]()> ByRef lpwcx As WNDCLASSEX) As <MarshalAs(UnmanagedType.U2)> Short
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function DefWindowProc(ByVal hWnd As IntPtr, ByVal uMsg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function UnregisterClass(ByVal lpClassName As String, ByVal hInstance As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function LoadCursor(ByVal hInstance As IntPtr, ByVal lpCursorName As Integer) As IntPtr
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function DestroyWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function




        <DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
        Public Shared Function UpdateLayeredWindow(ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, _
                                               ByRef pptDst As PointEx, ByRef psize As SizeEx, _
                                               ByVal hdcSrc As IntPtr, ByRef pptSrc As PointEx, _
                                               ByVal crKey As UInteger, <[In]()> ByRef pblend As BLENDFUNCTION, _
                                               ByVal dwFlags As UInteger) As Boolean
        End Function

        '<DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
        'Public Shared Function UpdateLayeredWindow(ByVal hwnd As IntPtr, ByVal hdcDst As IntPtr, ByRef pptDst As Drawing.Point, ByRef psize As SIZE, ByVal hdcSrc As IntPtr, ByRef pptSrc As point, ByVal crKey As Int32, ByRef pblend As BLENDFUNCTION, ByVal dwFlags As Int32) As Integer
        'End Function


        <DllImport("user32.dll")> _
        Public Shared Function IsWindow(ByVal hWnd As IntPtr) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function


        Public Shared Function SetWindowLongPtr(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As IntPtr) As IntPtr
            If IntPtr.Size = 8 Then
                Return SetWindowLongPtr64(hWnd, nIndex, dwNewLong)
            Else
                Return New IntPtr(SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()))
            End If
        End Function

        <DllImport("user32.dll", EntryPoint:="SetWindowLongW")> _
        Private Shared Function SetWindowLong32(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
        End Function

        <DllImport("user32.dll", EntryPoint:="SetWindowLongPtrW")> _
        Private Shared Function SetWindowLongPtr64(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As IntPtr) As IntPtr
        End Function

        <DllImport("user32.dll", EntryPoint:="GetWindowLongW")> _
        Private Shared Function GetWindowLongPtr32(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
        End Function

        <DllImport("user32.dll", EntryPoint:="GetWindowLongPtrW")> _
        Private Shared Function GetWindowLongPtr64(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
        End Function

        Public Shared Function GetWindowLongPtr(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As IntPtr
            If IntPtr.Size = 8 Then
                Return GetWindowLongPtr64(hWnd, nIndex)
            Else
                Return New IntPtr(GetWindowLongPtr32(hWnd, nIndex))
            End If
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function CallWindowProc(ByVal lpPrevWndFunc As IntPtr, ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function GetCaretPos(ByRef lpPoint As PointEx) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function GetGUIThreadInfo(ByVal idThread As UInteger, ByRef lpgui As GUITHREADINFO) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function ClientToScreen(ByVal hWnd As IntPtr, ByRef lpPoint As PointEx) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function ScreenToClient(ByVal hWnd As IntPtr, ByRef lpPoint As PointEx) As <MarshalAs(UnmanagedType.Bool)> Boolean
        End Function

        <DllImport("user32.dll")> _
        Public Shared Function ShowWindow(ByVal hWnd As IntPtr, ByVal nCmdShow As Integer) As Boolean
        End Function

        ''' <summary>
        ''' 该函数能在显示与隐藏窗口时能产生特殊的效果。有两种类型的动画效果：滚动动画和滑动动画。
        ''' </summary>
        ''' <param name="whnd">指定产生动画的窗口的句柄</param>
        ''' <param name="dwtime">指明动画持续的时间（以微秒计），完成一个动画的标准时间为200微秒</param>
        ''' <param name="dwflag">指定动画类型。这个参数可以是一个或多个下列标志的组合。标志描述：<br />
        ''' AW_SLIDE：使用滑动类型。缺省则为滚动动画类型。当使用AW_CENTER标志时，这个标志就被忽略。<br />
        ''' AW_ACTIVE：激活窗口。在使用了AW_HIDE标志后不要使用这个标志。<br />
        ''' AW_BLEND：使用淡出效果。只有当hWnd为顶层窗口的时候才可以使用此标志。<br />
        ''' AW_HIDE：隐藏窗口，缺省则显示窗口。<br />
        ''' AW_CENTER：若使用了AW_HIDE标志，则使窗口向内重叠；若未使用AW_HIDE标志，则使窗口向外扩展。
        ''' AW_HOR_POSITIVE：自左向右显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用AW_CENTER标志时，该标志将被忽略。<br />
        ''' AW_VER_POSITIVE：自顶向下显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用AW_CENTER标志时，该标志将被忽略。<br />
        ''' AW_VER_NEGATIVE：自下向上显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用AW_CENTER标志时，该标志将被忽略。<br />
        ''' AW_HOR_NEGATIVE：自右向左显示窗口。该标志可以在滚动动画和滑动动画中使用。当使用AW_CENTER标志时，该标志将被忽略。<br />
        ''' </param>
        ''' <returns>如果函数成功，返回值为非零；如果函数失败，返回值为零。在下列情况下函数将失败</returns>
        ''' <remarks>可以将AW_HOR_POSITIVE或AW_HOR_NEGTVE与AW_VER_POSITVE或AW_VER_NEGATIVE组合来激活一个窗口</remarks>
        <DllImport("user32")> _
        Public Shared Function AnimateWindow(ByVal whnd As IntPtr, ByVal dwtime As Integer, ByVal dwflag As Integer) As Boolean
        End Function

        ''' <summary>
        ''' Locks the workstation's display. Locking a workstation protects it from unauthorized use.
        ''' </summary>
        ''' <returns>If the function succeeds, the return value is nonzero. 
        ''' Because the function executes asynchronously, 
        ''' a nonzero return value indicates that the operation has been initiated. 
        ''' It does not indicate whether the workstation has been successfully locked.
        ''' </returns>
        ''' <remarks></remarks>
        <DllImport("user32.dll")> _
        Public Shared Function LockWorkStation() As Integer
        End Function

        ''' <summary>
        ''' The BlockInput function blocks keyboard and mouse input events from reaching applications. 
        ''' </summary>
        ''' <param name="value"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("user32.dll")> _
        Public Shared Function BlockInput(ByVal value As Boolean) As Integer
        End Function


        ''' <summary>
        ''' 注册热键
        ''' </summary>
        ''' <param name="hWnd">为本程序窗口的句柄，在C#的窗口中直接用Handle属性就可以引用窗口句柄</param>
        ''' <param name="id">为热键的标示符，是我们自己定义的，因为一个程序中可以定义多个热键，所以要用这个字段来区别，具体用法见下</param>
        ''' <param name="fsModifiers">为激活热键时，是否和系统键组合使用，none:0 Alt:1 Ctrl:2 Shift:4,并且还可以用或运算来组合使用</param>
        ''' <param name="vk">就是要定义的热键，VB.NET中按键都被包含在Keys枚举中</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("user32.dll")> _
        Public Shared Function RegisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer, ByVal fsModifiers As KeyModifiers, ByVal vk As Keys) As Boolean
        End Function

        ''' <summary>
        ''' 卸载热键
        ''' </summary>
        ''' <param name="hWnd">为本程序窗口的句柄，在C#的窗口中直接用Handle属性就可以引用窗口句柄</param>
        ''' <param name="id">要卸载的热键标示</param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("user32.dll")> _
        Public Shared Function UnregisterHotKey(ByVal hWnd As IntPtr, ByVal id As Integer) As Boolean
        End Function


        <DllImport("user32.dll", ExactSpelling:=True, SetLastError:=True)> _
        Public Shared Function GetDC(ByVal hWnd As IntPtr) As IntPtr
        End Function


        <DllImport("user32.dll", ExactSpelling:=True)> _
        Public Shared Function ReleaseDC(ByVal hWnd As IntPtr, ByVal hDC As IntPtr) As Integer
        End Function

        ''' <summary>
        ''' 设置为前端窗口
        ''' </summary>
        ''' <param name="hWnd"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <DllImport("user32.dll", ExactSpelling:=True)> _
        Public Shared Function SetForegroundWindow(ByVal hWnd As IntPtr) As Boolean
        End Function

    End Class

End Namespace